home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Delphi Magazine Collection 2001
/
Delphi Magazine Collection 20001 (2001).iso
/
DISKS
/
ISSUE09
/
FILES
/
STRM1U.PAS
< prev
Wrap
Pascal/Delphi Source File
|
1996-02-27
|
3KB
|
169 lines
unit Strm1u;
interface
uses
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
Forms, Dialogs, StdCtrls, ExtCtrls, Buttons;
type
TPointData = class
public
X, Y: Word;
constructor CreateXY(AX, AY: Word);
procedure SwapXY;
procedure LoadFromStream(Stream: TStream); virtual;
procedure SaveToStream(Stream: TStream); virtual;
end;
TForm1 = class(TForm)
PaintBox1: TPaintBox;
Bevel1: TBevel;
MakeBtn: TButton;
SaveBtn: TButton;
LoadBtn: TButton;
SwapBtn: TButton;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure PaintBox1Paint(Sender: TObject);
procedure MakeBtnClick(Sender: TObject);
procedure SaveBtnClick(Sender: TObject);
procedure LoadBtnClick(Sender: TObject);
procedure SwapBtnClick(Sender: TObject);
private
PointList: TList;
procedure ClearPoints;
end;
var
Form1: TForm1;
Pt: TPointData;
Loop: Integer;
const
DataFile = 'POINTS1.DAT';
implementation
{$R *.DFM}
constructor TPointData.CreateXY(AX, AY: Word);
begin
inherited Create;
X := AX;
Y := AY;
end;
procedure TPointData.SwapXY;
var
Tmp: Word;
begin
Tmp := X;
X := Y;
Y := Tmp;
end;
procedure TPointData.LoadFromStream(Stream: TStream);
begin
Stream.Read(X, SizeOf(X));
Stream.Read(Y, SizeOf(Y));
end;
procedure TPointData.SaveToStream(Stream: TStream);
begin
Stream.Write(X, SizeOf(X));
Stream.Write(Y, SizeOf(Y));
end;
procedure TForm1.ClearPoints;
begin
while PointList.Count > 0 do
begin
TPointData(PointList[0]).Free;
PointList.Delete(0);
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
PointList := TList.Create;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
ClearPoints;
PointList.Free;
end;
procedure TForm1.PaintBox1Paint(Sender: TObject);
begin
for Loop := 0 to PointList.Count - 1 do
begin
Pt := TPointData(PointList.Items[Loop]);
if Loop = 0 then
PaintBox1.Canvas.MoveTo(Pt.X, Pt.Y)
else
PaintBox1.Canvas.LineTo(Pt.X, Pt.Y)
end;
end;
procedure TForm1.MakeBtnClick(Sender: TObject);
begin
ClearPoints;
for Loop := 1 to Random(40) + 1 do
begin
Pt := TPointData.CreateXY(Random(PaintBox1.Width),
Random(PaintBox1.Height));
PointList.Add(Pt);
PaintBox1.Invalidate;
end;
end;
procedure TForm1.SaveBtnClick(Sender: TObject);
var
Stream: TFileStream;
begin
Stream := TFileStream.Create(DataFile, fmCreate);
try
for Loop := 0 to PointList.Count - 1 do
begin
Pt := TPointData(PointList.Items[Loop]);
Pt.SaveToStream(Stream);
end;
finally
Stream.Free;
end;
ClearPoints;
PaintBox1.Invalidate;
end;
procedure TForm1.LoadBtnClick(Sender: TObject);
var
Stream: TFileStream;
begin
ClearPoints;
Stream := TFileStream.Create(DataFile, fmOpenRead or fmShareDenyWrite);
try
while Stream.Position <> Stream.Size do
begin
Pt := TPointData.Create;
Pt.LoadFromStream(Stream);
PointList.Add(Pt);
end;
finally
Stream.Free;
end;
PaintBox1.Invalidate;
end;
procedure TForm1.SwapBtnClick(Sender: TObject);
begin
for Loop := 0 to PointList.Count - 1 do
TPointData(PointList.Items[Loop]).SwapXY;
PaintBox1.Invalidate;
end;
initialization
Randomize;
end.